package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.po.Lottery;
import com.cn.goodsadmin.po.Realgoods;
import com.cn.goodsadmin.po.Realprize;
import com.cn.goodsadmin.service.LotteryService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class LotteryController {

    @Autowired
    LotteryService lotteryService;


    @LoginRequired
    @RequestMapping(value = "/addlottery", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addLottery(@RequestBody Lottery lottery) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.save(lottery);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/updatelottery", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateLottery(@RequestBody Lottery lottery) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.update(lottery);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/deletelottery", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteLottery(@RequestBody Lottery lottery) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.delete(lottery);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/addlotteryprize", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addLotteryPrize(@RequestBody Realprize realprize) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.savePrize(realprize);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/deletelotteryprize", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteLotteryPrize(@RequestBody Realprize realprize) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.deletePrize(realprize);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/getallprize", method = RequestMethod.GET)
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getallPrize() {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Realprize> realprizes = lotteryService.getAllPrize();
            if (realprizes != null) {
                supplyMap.put("realprize", realprizes);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/getalllottery", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getallLottery(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Lottery> lotteries = lotteryService.getAllLottery(appid);
            if (lotteries != null) {
                supplyMap.put("lotteries", lotteries);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/realgoods", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getrealGoods(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Realgoods> realgoods = lotteryService.getAllRealGoods(appid);
            if (realgoods != null) {
                supplyMap.put("realgoods", realgoods);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/addrealgoods", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addRealgoods(@RequestBody Realgoods realgoods) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.saveRealGoods(realgoods);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/updaterealgoods", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateRealgoods(@RequestBody Realgoods realgoods) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.updateRealGoods(realgoods);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    //@LoginRequired
    @RequestMapping(value = "/deleterealgoods", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteRealgoods(@RequestBody Realgoods realgoods) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            lotteryService.deleteRealGoods(realgoods);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
